#pragma once
#include "wnd.h"

namespace mini
{
	namespace ui
	{
		class CList : public CWnd
		{
			friend class CUIMain;
		public:
			enum ControlMode
			{
				tree,
				list,
				box,
			};
			struct listItem
			{
				WORD					flag;
				bool					mbDrop = false;
				bool					mbUpdate = false;
				CWnd*					mpWnd = nullptr;
				listItem*				mParentListItem = nullptr;
				std::string				mText;
				std::list<listItem*>	mpChildItem;
			};

		public:
			CList();
			explicit CList(const std::string text);
			virtual ~CList();
		
			CList::listItem*	findRootItem(const std::string& name);
			std::pair<CList::listItem*, int> findChildItem(CList::listItem* pItem, const std::string& name, int nBegin = -1);
			CList::listItem* addRootItem(const std::string& name);
			void addItem(CList::listItem* pItem, const std::string& name);
			void deleteItem(CWnd* pItem);
			void deleteItem(int nPosition);
			void deleteItem(int nPosition, int nBegin);
			void deleteAllItem();
			void deleteAllItem(CList::listItem* pItems);
			void selectItem(CList::listItem* pItem);
			void doubleClickItem(CList::listItem* pItem);
			void mouseWheel(int iMessage);
			void mouseMove(int x, int y);
			void setSelectContentFunc(std::function<void(CList* pList, CList::listItem*)> func);
			void setSelectItemFunc(std::function<void(bool,CList::listItem*)> func);
			void setDoubleClickItemFunc(std::function<void(bool, CList::listItem*)> func);
			void setMouseWheelFunc(std::function<void(int)> func);
			void setMouseMoveFunc(std::function<void(int,int)> func);
			CList::listItem*	getChooseItem();
			void		setMode(ControlMode mode);
			ControlMode getMode();

		private:
			CList::listItem* createItem(const std::string& name);
			CList::listItem* _createItemTree(const std::string& name);
			CList::listItem* _createItemList(const std::string& name);
			CList::listItem* _createItemBox(const std::string& name);
			CList::listItem* _checkListItem(CList::listItem* pItemList, event& e);
			int _updateItemPlace(std::list<CList::listItem*>& pItems, int xOffset, int yOffset);

		private:
			listItem	mListItem;
			listItem*	mpChooseItem = nullptr;
			CWnd*		mpBGWnd = nullptr;
			float		mBoxItemSize = 128;
			ControlMode	mMode = list;
			std::function<void(CList*, CList::listItem*)>	mfuncUpdate;
			std::function<void(bool, CList::listItem*)>	mfuncSelect;
			std::function<void(bool, CList::listItem*)>	mfuncDoubleSelect;
			std::function<void(int)>					mfuncMouseWheel;
			std::function<void(int, int)>				mfuncMouseMove;
		};

	}
}
typedef std::function<void(mini::ui::CList*, mini::ui::CList::listItem*)> FuncSelect;